package A3D_Test {

	import A3D_Test.Logger;
	import A3D_Test.ModelInstance;
	import A3D_Test.ModelResource;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.lights.AmbientLight;
	import flash.display.Sprite;
	import flash.display.Stage3D;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	
	public class Main extends Sprite {

		public function Main() {
			Logger.Setup( stage );
			
			Logger.Log( "Started" );
			Logger.Log( ( new String()).concat( "Stage: ", stage.width, "x", stage.height ) );
			
			stage.align = StageAlign.LEFT;
			stage.scaleMode = StageScaleMode.SHOW_ALL;
			
			SetupLoginScreen();
			  
			Logger.Log( "Constructor done" );
		}
		

		
		private function SetupLoginScreen():void
		{
			loginBox = new TextField;
			loginBox.type = TextFieldType.INPUT;
			loginBox.selectable = true;
			loginBox.textColor = 0x2FEAFF;
			loginBox.width = 300;
			loginBox.height = 36;
			loginBox.border = true;
			loginBox.borderColor = 0x00D5D5;
			loginBox.defaultTextFormat = new TextFormat( "Arial", 24, 0x00FFFF, true, false, false, null, null, TextFormatAlign.CENTER );
			loginBox.restrict = "A-Za-z0-9";
			loginBox.text = "Player";
			loginBox.setSelection(0, 1000);
			loginBox.addEventListener( KeyboardEvent.KEY_DOWN, onLoginKeyDown );
			stage.addChild( loginBox );
			loginBox.x = stage.stageWidth / 2 - loginBox.width / 2;
			loginBox.y = stage.stageHeight / 2 - loginBox.height / 2;
			stage.focus = loginBox;
		}
		
		
		private function onLoginKeyDown( e:KeyboardEvent ):void
		{
			if ( e.keyCode == Keyboard.ENTER ) {
				stage.removeChild( loginBox );
				
				svGate = new ServerGate( loginBox.text );
				svGate.addEventListener( ServerGateEvent.LOGIN_RESULT, onLoginResult );
				svGate.addEventListener( ServerGateEvent.TRANSPORT_FAILURE, onTransportFailure );
			}
		}
		
		
		private function onTransportFailure( e:ServerGateFailureEvent ):void
		{
			Logger.Log( "Transport failed: " + e.errorMessage );
			
			SetupLoginScreen();
		}
		
		
		private function onLoginResult( e:ServerGateLoginResultEvent ):void
		{
			StartGame();
		}
		
		
		private function StartGame():void
		{
			stage3D = stage.stage3Ds[0];
			stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
			stage3D.requestContext3D();
			
			world = new World( svGate );
		}

		
		
		private function onContextCreate(e:Event):void {
			stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreate);

			Logger.Log( "Context created..." );
			
			client = new LocalClient( stage3D.context3D, stage, stage.frameRate );
			addChild( client.camera.view );
			addChild( client.camera.diagram );
			rootContainer.addChild( client );

			var ambl:AmbientLight = new AmbientLight( 0xDDACCB );
			ambl.intensity = 0.3;
			
			rootContainer.addChild( ambl );

			var stationRes:ModelResource = new ModelResource( stage3D.context3D, "st.dae" );
			var stationMdl:ModelInstance = new ModelInstance( stationRes );
			rootContainer.addChild( stationMdl );
			
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			positionSendTimer = new Timer( 500 );
			positionSendTimer.start();
			positionSendTimer.addEventListener( TimerEvent.TIMER, onPositionSendTimer );
		}

		
		private function onEnterFrame(e:Event):void {
			client.camera.render( stage3D ); //FXIME: icapsulate in LocalClient ?
		}
		
		private function onPositionSendTimer(e:Event):void {
			svGate.protoSendCoords( client.x, client.y, client.z );
		}
		
		private var rootContainer:Object3D = new Object3D();
		private var stage3D:Stage3D;
		private var client:LocalClient;
		private var svGate:ServerGate;
		private var loginBox:TextField;
		
		private var positionSendTimer:Timer;
		
		private var world:World;
	}
}
